package com.etc.zmall.dao.impl;

import java.util.List;

import com.etc.util.DBUtil;
import com.etc.util.PageData;
import com.etc.zmall.dao.GoodsDao;
import com.etc.zmall.entity.Goods;

/**
 * Goods 商品DAO实现类
 * 
 * @author knowno
 *
 */
public class GoodsDaoImpl implements GoodsDao {
	/**
	 * 增加商品 Goods 商品对象
	 */
	@Override
	public boolean addGoods(Goods goods) {
		String sql = "insert into tbl_goods values(null,?,?,?,?,?,?,?,?)";
		return DBUtil.exUpdate(sql, goods.getGoodsname(), goods.getGoodsprice(), goods.getGoodsinfo(),
				goods.getGoodscount(), goods.getCover(), goods.getTypeid(), goods.getShopid(), goods.getGoodsstate());
	}

	/**
	 * 删除商品
	 * 
	 */
	@Override
	public boolean delGoods(int goodsid) {
		String sql = "delete from tbl_goods where goodsid=?";
		return DBUtil.exUpdate(sql, goodsid);
	}

	/**
	 * 修改商品信息
	 */
	@Override
	public boolean upGoods(String goodsname, double goodsprice, String goodsinfo, int goodscount, String cover,
			int typeid, int shopid, int goodsstate, int goodsid) {

		String sql = "update tbl_goods set goodsname =?,goodsprice=?,goodsinfo=?,goodscount=?,cover=?,typeid=?,shopid=?,goodsstate=? where goodsid=?";
		return DBUtil.exUpdate(sql, goodsname, goodsprice, goodsinfo, goodscount, cover, typeid, shopid, goodsstate,
				goodsid);
	}

	/**
	 * 修改商品状态 上下架
	 */
	@Override
	public boolean updateGoods(int goodsid, int goodsstate) {
		String sql = "update tbl_goods set goodsstate =? where goodsid=?";
		return DBUtil.exUpdate(sql, goodsstate, goodsid);
	}

	/**
	 * 搜索商品名称 (模糊查询)
	 */
	@Override
	public PageData<Goods> queryGoods(int page, int pageSize, String keywords) {
		String sql = "select goodsid,goodsname,goodsprice,goodsinfo,goodscount,goodsstate,cover,shopid from tbl_goods where goodsname like ?";
		return DBUtil.exQueryByPage(sql, Goods.class, page, pageSize, '%' + keywords + '%');
	}

	/**
	 * 搜索商品名称 (模糊查询)
	 */
	@Override
	public PageData<Goods> queryGoodsShopLike(int page, int pageSize, int shopid, String keywords) {
		String sql = "select tbl_goods.goodsid,tbl_goods.goodsname,tbl_goods.goodsprice,tbl_goods.goodsinfo,tbl_goods.goodscount,tbl_goods.cover,tbl_goods.shopid,tbl_goods.goodsstate,tbl_goodstype.typeid,tbl_goodstype.typename,tbl_goodstype.typeinfo from tbl_goods inner join tbl_goodstype on tbl_goods.typeid=tbl_goodstype.typeid \r\n"
				+ " where (tbl_goods.goodsname like ? or tbl_goods.goodsinfo like ?) and tbl_goods.shopid=?";
		return DBUtil.exQueryByPage(sql, Goods.class, page, pageSize, '%' + keywords + '%', '%' + keywords + '%',
				shopid);
	}

	/**
	 * 
	 * 商品类别查询(商品类别id)
	 */
	@Override
	public PageData<Goods> queryGoodsType(int page, int pageSize, int typeid) {
		String sql = "select * from tbl_goods where typeid=?";
		return DBUtil.exQueryByPage(sql, Goods.class, page, pageSize, typeid);
	}

	/**
	 * 商铺的所以商品(根据商店id)
	 */
	@Override
	public PageData<Goods> queryGoodsShopid(int page, int pageSize, int shopid) {
		String sql = "select tbl_goods.goodsid,tbl_goods.goodsname,tbl_goods.goodsprice,tbl_goods.goodsinfo,tbl_goods.goodscount,tbl_goods.cover,tbl_goods.shopid,tbl_goods.goodsstate,tbl_goodstype.typeid,tbl_goodstype.typename,tbl_goodstype.typeinfo from tbl_goods inner join tbl_goodstype on tbl_goods.typeid=tbl_goodstype.typeid where tbl_goods.shopid=?";
		return DBUtil.exQueryByPage(sql, Goods.class, page, pageSize, shopid);
	}

	/**
	 * 查询商品详细信息(商品详情)
	 */
	@Override
	public Goods queryOneGoods(int goodsid) {
		String sql = "select * from tbl_goods where goodsid = ?";
		List<Goods> list = (List<Goods>) DBUtil.exQuery(sql, Goods.class, goodsid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 查询所有商品表
	 */
	@Override
	public List<Goods> queryAllGoods() {
		String sql = "select * from tbl_goods";
		List<Goods> list = (List<Goods>) DBUtil.exQuery(sql, Goods.class);
		return list;
	}

}
